package com.wnd.dell.myapplication;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dell on 2017/7/4.
 */

public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private WeatherData weatherData;


    public MyAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(WeatherData weatherData) {
        this.weatherData = weatherData;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (weatherData == null) {
            return 0;
        } else {
            return weatherData.getResult().getFuture().size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        ViewHolder1 holder = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_layout, null);
            holder = new ViewHolder1();
            holder.textView = (TextView) convertView.findViewById(R.id.tv_Temperature);
            holder.textView2 = (TextView) convertView.findViewById(R.id.tv_Date);
            holder.textView3 = (TextView) convertView.findViewById(R.id.tv_Weather);
            holder.textView4 = (TextView) convertView.findViewById(R.id.tv_Week);
            holder.textView5 = (TextView) convertView.findViewById(R.id.tv_Wind);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_head);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder1) convertView.getTag();
        }
        holder.textView.setText(weatherData.getResult().getFuture().get(position).getTemperature());
        holder.textView2.setText(weatherData.getResult().getFuture().get(position).getDate());
        holder.textView3.setText(weatherData.getResult().getFuture().get(position).getWeather());
        holder.textView4.setText(weatherData.getResult().getFuture().get(position).getWeek());
        holder.textView5.setText(weatherData.getResult().getFuture().get(position).getWind());
        return convertView;

    }

    public static class ViewHolder1 {
        public TextView textView;
        public TextView textView2;
        public TextView textView3;
        public TextView textView4;
        public TextView textView5;
        public ImageView imageView;
    }
}
